<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use think\facade\Db;
use think\facade\View;

class Common extends Base{
    /*查找子分类
    * 额外加了数组字段：child是否含有子类，hide_id点击要隐藏的tr_id行
    */
    public function child(){
        // 转为小写字母
        $general = new \common\General();
        $tabledir = $general->capital_to_underline(input('tabledir'));

        $id = input('id');
        $where[] = ['parentid','=',$id];
        switch ($tabledir) {
            case 'area':
                $field = 'id,parentid,title,level,sequence,is_domain,bind_domain';
                $rsArr = Db::name($tabledir)->field($field)->where($where)->order('sequence desc')->select();
                break;
            case 'about':
                $field = 'id,parentid,title,level,sequence,thumb,urlroute,signid';
                $rsArr = Db::view($tabledir,$field)->view('sort',['title'=>'s_title'],"sort.id=$tabledir.sortid")
                       ->where("$tabledir.parentid",$id)->select();
                break;
            //默认是sort表
            default:
                $field = 'id,parentid,tabledir,title,level,sequence,thumb,urlroute';
                $rsArr = Db::name($tabledir)->field($field)->where($where)->select();
                break;
        }

        //再次查找子分类
        if(!empty($rsArr)){
            $hide_id = '';
            $hide_id_arr = getOneArr('getChildSort',$tabledir,'id,parentid',$id,'id','arr');
            foreach ($hide_id_arr as $hide_id_rs) {
                $hide_id .= $hide_id_rs.',';
            }
            $hide_id = rtrim($hide_id,',');
            foreach ($rsArr as $rs) {
                $childRs = Db::name($tabledir)->where('parentid',$rs['id'])->find();
                if(!empty($childRs)){
                    $child = array('child'=>'yes');
                }else{
                    $child = array('child'=>'no');
                }
                $rs += $child + array('hide_id'=>$hide_id);
                $rsData[] = $rs;
            }
        }else{
            $rsData = array();
        }

        return json($rsData);
    }
}